//
//  AppInfoParser.m
//  SandBayCinema
//
//  Created by Rayco on 12-10-18.
//  Copyright (c) 2012年 Apps123. All rights reserved.
//

#define BUNDLE @"bundle"
#define PROJECT_BUNDLE @"ProjectInfo/projectinfo"
#define INFO_TYPE @"info"

#define PROJECT_BUNDLE_PATH  [NSString stringWithFormat:@"%@/%@/%@",T_PROJECT_PATH,T_PROJECT_NAME,BUNDLE_NAME]

#define PROJECT_INFO_PATH @"doc/project"
#define DATA_INFO_PATH @"doc/data"

#import "AppsProjectInfoParser.h"
#import "JSON.h"
#import "apps123_util.h"

@implementation AppsProjectInfoParser

+ (ProjectInfo *)parseProjectInfo {
   
    NSString *bundlePath = PROJECT_BUNDLE_PATH;//[[NSBundle mainBundle] pathForResource:PROJECT_BUNDLE ofType:BUNDLE];
    NSBundle *projectinfoBundle = [NSBundle bundleWithPath:bundlePath];
    NSString *projectInfo = [[NSString alloc] initWithContentsOfFile:[projectinfoBundle pathForResource:PROJECT_INFO_PATH ofType:INFO_TYPE] encoding:NSUTF8StringEncoding error:nil];
    
    ProjectInfo *info = [[ProjectInfo alloc] init];
    NSDictionary *projectInfoDic = [[projectInfo copy] JSONValue];
    if(projectInfoDic){
        
        info.appsDesc = [projectInfoDic objectForKey:@"appsDesc"];
        info.appsDisplayName = [projectInfoDic objectForKey:@"appsDisplayName"];
        info.appsName = [projectInfoDic objectForKey:@"appsName"];
        info.icon = [projectInfoDic objectForKey:@"icon"];
        info.keyword = [projectInfoDic objectForKey:@"keyword"];
        info.merchantID = [projectInfoDic objectForKey:@"merchantID"];
        info.projectID = [projectInfoDic objectForKey:@"projectID"];
        info.website = [projectInfoDic objectForKey:@"website"];
        info.webAppName = [projectInfoDic objectForKey:@"webAppName"];
        info.webAppShortName = [projectInfoDic objectForKey:@"webAppShortName"];
        info.version = [projectInfoDic objectForKey:@"version"];
        
        if([projectInfoDic objectForKey:@"screenshots"]){
            [info.screenshots addObjectsFromArray:[projectInfoDic objectForKey:@"screenshots"]];
        }
        if([projectInfoDic objectForKey:@"platform"]){
            [info.platforms addObjectsFromArray:[projectInfoDic objectForKey:@"platform"]];
        }
        
        return info;
    }
    return nil;
}

+ (DataInfo *)parseDataInfo {
    NSString *bundlePath = PROJECT_BUNDLE_PATH;//[[NSBundle mainBundle] pathForResource:PROJECT_BUNDLE ofType:@"bundle"];
    NSBundle *datainfoBundle = [NSBundle bundleWithPath:bundlePath];
    NSString *dataInfo = [[NSString alloc] initWithContentsOfFile:[datainfoBundle pathForResource:DATA_INFO_PATH ofType:INFO_TYPE] encoding:NSUTF8StringEncoding error:nil];
    
    DataInfo *info = [[DataInfo alloc] init];
    NSDictionary *dataInfoDic = [[dataInfo copy] JSONValue];
    if(dataInfoDic){
        
        info.requestedType = [dataInfoDic objectForKey:@"requestedType"];
        info.tomcatUrl = [dataInfoDic objectForKey:@"tomcatUrl"];
        info.navBar = [apps123_util abstractLastFix:[dataInfoDic objectForKey:@"navBar"]];

        NSMutableArray *dataList = [dataInfoDic objectForKey:@"dataList"];
        if(dataList && [dataList count] > 0){ 
            NSDictionary *splashPageData = [dataList objectAtIndex:0];
            
            if(splashPageData){
                SplashDataListItem *splashDataItem = [[SplashDataListItem alloc] init];
                
                NSString *filePath = [splashPageData objectForKey:@"filePath"];
                splashDataItem.filePath = filePath;
                
                NSDictionary *data = [splashPageData objectForKey:@"data"];
                if(data){
                    NSString *location = [data objectForKey:@"location"];
                    NSString *url = [data objectForKey:@"url"];
                    NSString *animationLength = [data objectForKey:@"animationLength"];
                    NSString *interval = [data objectForKey:@"interval"];
                    NSMutableArray *animation = [data objectForKey:@"animation"];
                    [splashDataItem.animation addObject:animation];

                    splashDataItem.location = location;
                    splashDataItem.url = url;
                    splashDataItem.interval = interval;
                    splashDataItem.animationLength = animationLength;
                    
                    [info.dataList addObject:splashDataItem];
                }
            }
            
        }
        if(dataList && [dataList count] > 1){
            NSDictionary *homePageData = [dataList objectAtIndex:1];
            
            if(homePageData){
                HomePageDataListItem *homePageItem = [[HomePageDataListItem alloc] init];
                
                NSMutableArray *filePath = [homePageData objectForKey:@"filePath"];
                [homePageItem.filePath addObjectsFromArray:filePath];
                homePageItem.sysTabName = [homePageData objectForKey:@"sysTabName"];
                homePageItem.layout = [homePageData objectForKey:@"layout"];
                
                NSDictionary *data = [homePageData objectForKey:@"data"];
                if(data){
                    homePageItem.customizeTabId = [data objectForKey:@"customizeTabId"];
                    homePageItem.backgroundImg = [data objectForKey:@"background"];
                    homePageItem.menuColor = [data objectForKey:@"color"];
                    homePageItem.tabTitle = [data objectForKey:@"tabTitle"] ? [data objectForKey:@"tabTitle"] : @"";
                    homePageItem.hideFont = [data objectForKey:@"hideFont"];
                    homePageItem.logo = [data objectForKey:@"logo"];
                    homePageItem.appIcon = [data objectForKey:@"appIcon"];
                    homePageItem.menuBackground = [data objectForKey:@"menuBackground"];
                    homePageItem.detailUrl = [data objectForKey:@"detailUrl"];
                    
                    NSMutableArray *customizeTabsList = [data objectForKey:@"customizeTabsList"];
                    for(int i = 0;i < [customizeTabsList count]; i++){
                        NSDictionary *customizeTab = [customizeTabsList objectAtIndex:i];
                        if(customizeTab){
                            CustomizeTabsListItem *customizeTabListItem = [[CustomizeTabsListItem alloc] init];
                            
                            customizeTabListItem.isActive = [customizeTab objectForKey:@"active"];
                            customizeTabListItem.backGround = [customizeTab objectForKey:@"backGround"];
                            customizeTabListItem.content_EN = [customizeTab objectForKey:@"content_EN"];
                            customizeTabListItem.content_CN = [customizeTab objectForKey:@"content_CN"];
                            customizeTabListItem.folder = [customizeTab objectForKey:@"folder"];
                            customizeTabListItem.functionName = [customizeTab objectForKey:@"functionName"];
                            customizeTabListItem.iconURL = [customizeTab objectForKey:@"iconURL"];
                            customizeTabListItem.iconName = [customizeTab objectForKey:@"iconName"];
                            customizeTabListItem.iconID = [customizeTab objectForKey:@"iconID"];
                            customizeTabListItem.ID = [customizeTab objectForKey:@"ID"];
                            customizeTabListItem.isShared = [customizeTab objectForKey:@"isShared"];
                            customizeTabListItem.ivo = [customizeTab objectForKey:@"ivo"];
                            customizeTabListItem.projectId = [customizeTab objectForKey:@"projectId"];
                            customizeTabListItem.sort = [customizeTab objectForKey:@"sort"];
                            customizeTabListItem.sysTabName = [customizeTab objectForKey:@"sysTabName"];
                            customizeTabListItem.tabName = [customizeTab objectForKey:@"tabName"];
                            customizeTabListItem.tabTitle = [customizeTab objectForKey:@"tabTitle"];
                            customizeTabListItem.tabType = [customizeTab objectForKey:@"tabType"];
                            
                            NSDictionary *tabLayoutVODic = [customizeTab objectForKey:@"tabLayoutVO"];
                            if(tabLayoutVODic){
                                TabLayoutVO *tabLayoutVO = [[TabLayoutVO alloc] init];
                                tabLayoutVO.fileName = [tabLayoutVODic objectForKey:@"fileName"];
                                tabLayoutVO.folder = [tabLayoutVODic objectForKey:@"folder"];
                                tabLayoutVO.ID = [tabLayoutVODic objectForKey:@"ID"];
                                tabLayoutVO.imageURL = [tabLayoutVODic objectForKey:@"imageURL"];
                                tabLayoutVO.layoutFilePath = [tabLayoutVODic objectForKey:@"layoutFilePath"];
                                tabLayoutVO.layoutName = [tabLayoutVODic objectForKey:@"layoutName"];
                                tabLayoutVO.maxCount = [tabLayoutVODic objectForKey:@"maxCount"];
                                tabLayoutVO.pic1 = [tabLayoutVODic objectForKey:@"pic1"];
                                tabLayoutVO.picSize = [tabLayoutVODic objectForKey:@"picSize"];
                                tabLayoutVO.sort = [tabLayoutVODic objectForKey:@"sort"];
                                
                                
                                customizeTabListItem.tabLayoutVO = tabLayoutVO;
                            }
                            
                            NSDictionary *tabsVODic = [customizeTab objectForKey:@"tabsVO"];
                            if(tabsVODic){
                                TabsVO *tabsVO = [[TabsVO alloc] init];
                                tabsVO.isActive = [tabsVODic objectForKey:@"active"];
                                tabsVO.backGround = [tabsVODic objectForKey:@"backGround"];
                                tabsVO.content_CN = [tabsVODic objectForKey:@"content_CN"];
                                tabsVO.content_EN = [tabsVODic objectForKey:@"content_EN"];
                                tabsVO.defaultDisplay = [tabsVODic objectForKey:@"defaultDisplay"];
                                tabsVO.folder = [tabsVODic objectForKey:@"folder"];
                                tabsVO.functionName = [tabsVODic objectForKey:@"functionName"];
                                tabsVO.iconURL = [tabsVODic objectForKey:@"iconURL"];
                                tabsVO.ID = [tabsVODic objectForKey:@"ID"];
                                tabsVO.industryId = [tabsVODic objectForKey:@"industryId"];
                                tabsVO.isAdded = [tabsVODic objectForKey:@"isAdded"];
                                tabsVO.isAvailable = [tabsVODic objectForKey:@"isAvailable"];
                                tabsVO.isShared = [tabsVODic objectForKey:@"isShared"];
                                tabsVO.layoutID = [tabsVODic objectForKey:@"layoutID"];
                                tabsVO.maxInstance = [tabsVODic objectForKey:@"maxInstance"];
                                tabsVO.multiSelect = [tabsVODic objectForKey:@"multiSelect"];
                                tabsVO.previewImage = [tabsVODic objectForKey:@"previewImage"];
                                tabsVO.projectId = [tabsVODic objectForKey:@"projectId"];
                                tabsVO.sort = [tabsVODic objectForKey:@"sort"];
                                tabsVO.sysTabName = [tabsVODic objectForKey:@"sysTabName"];
                                tabsVO.tabName = [tabsVODic objectForKey:@"tabName"];
                                tabsVO.tabTitle = [tabsVODic objectForKey:@"tabTitle"];
                                tabsVO.tabType = [tabsVODic objectForKey:@"tabType"];
                                tabsVO.templateId = [tabsVODic objectForKey:@"templateId"];
                                tabsVO.tips = [tabsVODic objectForKey:@"tips"];
                                
                                
                                customizeTabListItem.tabsVO = tabsVO;
                            }
                            
                            [homePageItem.customizeTabsList addObject:customizeTabListItem];
                        }
                    }
                }
                
                [info.dataList addObject:homePageItem];
            }
            
        }
        if(dataList && [dataList count] > 2){
            for(int i = 2; i < [dataList count]; i++){
                NSDictionary *tabData = [dataList objectAtIndex:i];
                
                if(tabData){
                    TabDataListItem *tabPageItem = [[TabDataListItem alloc] init];
                    
                    NSMutableArray *filePath = [tabData objectForKey:@"filePath"];
                    [tabPageItem.filePath addObjectsFromArray:filePath];
                    tabPageItem.sysTabName = [tabData objectForKey:@"sysTabName"];
                    tabPageItem.layout = [tabData objectForKey:@"layout"];
                    tabPageItem.customizeId = [tabData objectForKey:@"customizeId"];
                    tabPageItem.customizeTabId = [tabData objectForKey:@"customizeId"];
                    
                    NSDictionary *data = [tabData objectForKey:@"data"];
                    if(data){
//                        tabPageItem.customizeTabId = [data objectForKey:@"customizeTabId"];
                        tabPageItem.homePageUrl = [data objectForKey:@"homePageUrl"];
                        tabPageItem.detailUrl = [data objectForKey:@"detailUrl"];
                        tabPageItem.level2Url = [data objectForKey:@"level2Url"];
                        tabPageItem.tabTitle = [data objectForKey:@"tabTitle"];
                        tabPageItem.location = [data objectForKey:@"location"];
                    }
//                    tabPageItem.sysTabName = [data objectForKey:@"sysTabName"];
//                    tabPageItem.customizeId = [data objectForKey:@"customizeId"];
//                    tabPageItem.layout = [data objectForKey:@"layout"];
                    
                    [info.dataList addObject:tabPageItem];
                }
                
            }
        }
        
        return info;
    }
    return nil;
}

@end
